package cn.yangliu.nacos.comm.config.security;

import cn.yangliu.nacos.comm.config.security.handler.AuthorityAspect;
import cn.yangliu.nacos.comm.config.security.handler.SecurityAccessDeniedHandler;
import cn.yangliu.nacos.comm.config.security.handler.SecurityAuthenticationEntryPoint;
import cn.yangliu.nacos.comm.config.security.handler.SecurityAuthenticationFailureHandler;
import cn.yangliu.nacos.comm.config.security.handler.SecurityAuthenticationSuccessHandler;
import cn.yangliu.nacos.comm.config.security.handler.SecurityLogoutSuccessHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * The type of nacos.
 *
 * @author 问道于盲
 * @date 2020 -01-01
 */
@ConditionalOnClass(Authentication.class)
@Configuration
@EnableConfigurationProperties(SecurityProperties.class)
public class SecurityConfig {

    /**
     * Password encoder password encoder.
     *
     * @return the password encoder
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    /**
     * Authority aspect authority aspect.
     *
     * @return the authority aspect
     */
    @Bean
    @ConditionalOnProperty(prefix = "security.commom", name = "enable", havingValue = "true", matchIfMissing = true)
    public AuthorityAspect authorityAspect() {
        return new AuthorityAspect();
    }

    /**
     * Access denied handler security access denied handler.
     *
     * @return the security access denied handler
     */
    @Bean
    @ConditionalOnProperty(prefix = "security.commom", name = "enable", havingValue = "true", matchIfMissing = true)
    public SecurityAccessDeniedHandler accessDeniedHandler() {
        return new SecurityAccessDeniedHandler();
    }


    /**
     * Authentication entry point security authentication entry point.
     *
     * @return the security authentication entry point
     */
    @Bean
    @ConditionalOnProperty(prefix = "security.commom", name = "enable", havingValue = "true", matchIfMissing = true)
    public SecurityAuthenticationEntryPoint authenticationEntryPoint() {
        return new SecurityAuthenticationEntryPoint();
    }

    /**
     * Authentication failure handler security authentication failure handler.
     *
     * @return the security authentication failure handler
     */
    @Bean
    public SecurityAuthenticationFailureHandler authenticationFailureHandler() {
        return new SecurityAuthenticationFailureHandler();
    }

    /**
     * Authentication success handler security authentication success handler.
     *
     * @return the security authentication success handler
     */
    @Bean
    public SecurityAuthenticationSuccessHandler authenticationSuccessHandler() {
        return new SecurityAuthenticationSuccessHandler();
    }

    /**
     * Logout success handler security logout success handler.
     *
     * @return the security logout success handler
     */
    @Bean
    @ConditionalOnProperty(prefix = "security.commom", name = "enable", havingValue = "true", matchIfMissing = true)
    public SecurityLogoutSuccessHandler logoutSuccessHandler() {
        return new SecurityLogoutSuccessHandler();
    }
}
